package com.example.demo.controller;

import com.example.demo.entity.PageBean;
import com.example.demo.entity.Result;
import com.example.demo.entity.Student;
import com.example.demo.entity.StudentQueryParam;
import com.example.demo.service.Impl.StudentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class StudentController {
    @Autowired
    private StudentServiceImpl studentService;

    /**
     * 分页查询
     * @param param
     * @return
     */
    @GetMapping("/students")
    public Result page(StudentQueryParam param){
        log.info(" StudentQueryParam 请求参数：{}", param);
        PageBean pageBean = studentService.page(param);
        return Result.success(pageBean);
    }

    /**
     * 新增student
     * @param student
     * @return
     */
    @PostMapping("/students")
    public Result save(@RequestBody Student student){
        log.info("请求参数：{}",student);
        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/students/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询:{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学生信息
     * @param student
     * @return
     */
    @PutMapping("/students")
    public Result update(@RequestBody Student student){
        log.info("修改学生信息：{}",student);
        studentService.update(student);
        return Result.success();
    }

    /**
     * 批量删除学生
     * @param ids
     * @return
     */
    @DeleteMapping("/students/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学生信息：{}",ids);
        studentService.delete(ids);
        return Result.success();
    }

    @PutMapping("/students/violation/{id}/{score}")
    public Result violationHandle( @PathVariable Integer id, @PathVariable Short score){
        log.info("处理学生违规信息：{}",id);
        studentService.violationHandle(id,score);
        return Result.success();
    }
}
